<?php

ini_set("display_errors", "On");
error_reporting(E_ALL);

// 引入数据库连接文件
require_once '../coon/connection.php';

$addName = $_POST['addName'];
// $addSaveAddress = $_POST['addSaveAddress'];

// 查询语句
$querySql = "select id from greeting_card_element where e_name='$addName'";
// 连接数据库，并查询
$result = mysqli_query($GLOBALS['conn'], $querySql);

//echo $result_config;
if (mysqli_num_rows($result) > 0) {
	echo "
<script>
    alert('该模板已存在，请重新输入！');
    history.go(-1);
</script>";
} else {
	// 模板文件
	if(!empty($_FILES)){
		$tmpName   = $_FILES['upFile']['tmp_name'];     // 临时文件名称
		$suffix      = explode(".",$_FILES['upFile']['name']);;         // 文件的后缀
		$path      = '../asset/img';                       // 上传目录
		$fileName = $addName.".".$suffix[1];
		// $file_name = date('YmdHis').rand(100,999).$name;// 避免文件重名，更改文件名称
		if(move_uploaded_file($tmpName, $path.'/'.$fileName)){
			// 后缀 suffix
			$now = date('Y-m-d H:i:s', time());
			$addSql = "INSERT INTO greeting_card_element(create_time, update_time,  e_name, saveAddress , file_name ,is_delete)VALUES ('$now','$now','$addName', '$path','$fileName',0)";
			
			if (mysqli_query($GLOBALS['conn'], $addSql)) {
				echo "
            <script>
                alert('添加成功！');
                window.location.href=document.referrer;
            </script>
        ";
			} else {
				echo "
            <script>
                alert('添加失败！');
                history.back();
            </script>
        ";
			}
			// echo $name." 上传成功！";
		}
	}
	
}

